package com.vessel.decoration.mvp.presenter.mine.manage;

import android.app.Application;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.vessel.decoration.app.constant.Constants;
import com.vessel.decoration.app.data.entity.ProductListResponse;
import com.vessel.decoration.app.data.entity.TypeListResponse;
import com.vessel.decoration.mvp.contract.mine.manage.BusinessContract;
import com.vessel.decoration.mvp.ui.adapter.ProductManagerAdapter;
import com.vessel.decoration.mvp.ui.listener.OnProductClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * @author vesselzhang
 * @date 2017/12/5
 */

@ActivityScope
public class BusinessPresenter extends BasePresenter<BusinessContract.Model, BusinessContract.View>
        implements OnProductClickListener {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;

    private ProductManagerAdapter mAdapter;
    private List<ProductListResponse.ProductsBean> mData;

    @Inject
    public BusinessPresenter(BusinessContract.Model model, BusinessContract.View rootView
            , RxErrorHandler handler, Application application, AppManager appManager
            , ProductManagerAdapter adapter, List<ProductListResponse.ProductsBean> list) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;

        this.mAdapter = adapter;
        this.mData = list;
        mAdapter.setOnProductClickListener(this);
    }

    public void getBusinessInfo() {
        mModel.queryShopInfo()
                .flatMap(shopInfoResponse -> {
                    mRootView.setShopInfo(shopInfoResponse);
                    queryTypeList(shopInfoResponse.getTypeId());
                    Map<String, Object> map = new HashMap<>();
                    map.put("shopId", shopInfoResponse.getShopId());
                    map.put("page", "1");
                    map.put("pageSize", "10");
                    return mModel.queryProductList(map);
                }).subscribe(
                new ErrorHandleSubscriber<ProductListResponse>(mErrorHandler) {
                    @Override
                    public void onNext(ProductListResponse productListResponse) {
                        mData.clear();
                        mData.addAll(productListResponse.getProducts());
                        mAdapter.notifyDataSetChanged();
                    }
                }
        );
    }

    public void queryTypeList(int typeId) {
        mModel.queryTypeList(1)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(
                        new ErrorHandleSubscriber<TypeListResponse>(mErrorHandler) {
                            @Override
                            public void onNext(TypeListResponse typeListResponse) {
                                for (TypeListResponse.TypesBean type : typeListResponse.getTypes()) {
                                    if (type.getTypeId() == typeId) {
                                        mRootView.showType(type.getTypeName());
                                        break;
                                    }
                                }
                            }
                        }
                );
    }

    @Override
    public void onItemClick(View view, ProductListResponse.ProductsBean product) {
        ARouter.getInstance().build("/app/business/product")
                .withSerializable(Constants.TYPE, Constants.TYPE_UPDATE)
                .withSerializable("data", product)
                .navigation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}